package com.atguigu.schedule.DAO.impl;

import com.atguigu.schedule.DAO.BaseDAO;
import com.atguigu.schedule.DAO.SysScheduleDAO;
import com.atguigu.schedule.pojo.SysSchedule;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class SysSysScheduleDAOImpl extends BaseDAO<SysSchedule> implements SysScheduleDAO {
    @Override
    public void insert(Connection conn, SysSchedule schedule) {
        String sql = "insert into sys_schedule(uid, title, completed) values(?,?,?)";
        update(conn, sql, schedule.getUid(), schedule.getTitle(), schedule.getCompleted());
    }

    @Override
    public void deleteById(Connection conn, int id) {
        String sql = "delete from sys_schedule where sid = ?";
        update(conn, sql, id);
    }

    @Override
    public void update(Connection conn, SysSchedule schedule) {
        String sql = "update sys_schedule set uid=?, title=?, completed=? where sid = ?";
        update(conn, sql, schedule.getUid(), schedule.getTitle(), schedule.getCompleted(), schedule.getSid());
    }

    @Override
    public SysSchedule getScheduleById(Connection conn, int id) {
        String sql = "select sid, uid, title, completed from sys_schedule where sid = ?";
        ArrayList<SysSchedule> schedules = query(conn, sql, id);
        if (!schedules.isEmpty()) {
            return schedules.get(0);
        }
        return null;
    }

    @Override
    public List<SysSchedule> getSchedules(Connection conn) {
        String sql = "select sid, uid, title, completed from sys_schedule";
        return query(conn, sql);
    }

    @Override
    public long getScheduleCount(Connection conn) {
        String sql = "select count(*) from sys_schedule";
        return getValue(conn, sql);
    }
}
